﻿using DotNetty.Buffers;
using DotNetty.Common.Utilities;
using DotNetty.Transport.Channels;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TestNetty.Service.Model;

namespace TestNetty.Service.Handlers
{
    internal class JsonServerHandler : SimpleChannelInboundHandler<MyMessage>
    {
        protected override void ChannelRead0(IChannelHandlerContext ctx, MyMessage msg)
        {
            if (msg is MyMessage myMessage)
            {
                // 处理接收到的MyMessage对象  
                System.Console.WriteLine($"Received message: Id={myMessage.Id}, Content={myMessage.Content}");

                // 响应消息，发送回客户端  
                var response = new MyMessage { Id = 1, Content = "Message received" };
                ctx.WriteAndFlushAsync(response);
            }
        }
    }
}
